package org.NooLab.itexx.retrieval;

import java.util.ArrayList;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.stuctures.RequestFocusContext;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.net.GUID;
import org.NooLab.utilities.strings.ArrUtilities;

public class RequestModifier {

	private ITexxWindowCommons appCommons;
	private RequestFocusContext requestFocus;

	ArrayList<DeliveryPackage> dps ;
	ArrayList<String> mands, excluds, normals;
	
	
	
	
	private PrintLog out;
	
	// ========================================================================
	public RequestModifier(ITexxWindowCommons commons) {
	
		appCommons = commons ;
		out = appCommons.out;
		requestFocus = appCommons.getModelController().getRequestFocusContext() ;
		
		
		
		mands   = requestFocus.getMandatoryItems();
		excluds = requestFocus.getExcludingItems() ;
		normals = requestFocus.getNormalItems();
		
		
	}
	// ========================================================================


	public void reset() {
		// 
		
	}



	public void setRequestFocus(RequestFocusContext rf) {
		// 
		requestFocus = rf;
	
		mands   = requestFocus.getMandatoryItems();
		excluds = requestFocus.getExcludingItems() ;
		normals = requestFocus.getNormalItems();

	}


	/**
	 * this creates variations for queries that are long;
	 * note that this may lead to double entries in the results 
	 * 
	 * @param dpItemIn
	 * @return
	 */
	public ArrayList<DeliveryPackage> generateQueryVariations(DeliveryPackage dpItemIn) {
		
		String groupGuid, querybase, dpQuery, rfQuery;
		
		dps = new ArrayList<DeliveryPackage>();
	
		dps.add(dpItemIn);
		
		// note that this contains ALL (=normals, mands, excluds)
		ArrayList<String> qt = dpItemIn.getQueryTerms();
		querybase = ArrUtilities.arr2Text( qt, "  ");
		
		if (qt.size()<10){
			return dps;
		}
		
		groupGuid = dpItemIn.getGroupGuid();
		
		if (excluds.size()>0){
			String xbase = ArrUtilities.arr2Text( excluds, "  -");		
			xbase = " -"+xbase.substring(0, xbase.length()-1).trim();
			querybase = querybase + xbase;
		}
		
		if ((normals.size()>0) && (normals.size()<=5)){
			
			String xbase = ArrUtilities.arr2Text( normals, "  ").trim();		
			
			querybase = xbase + " "+ querybase ;
		}
		
		
		for (int i=0;i<mands.size();i++){
			
			
			DeliveryPackage dp = new DeliveryPackage( dpItemIn ) ;
			
			dp.priority = dp.priority -2 ;
			dp.setGuid( GUID.randomvalue() ) ;
			
			dp.getQueryTerms().add( mands.get(i));
			if ((mands.size()>1) && (i<mands.size()-1)){
				dp.getQueryTerms().add( mands.get(i+1));
			}
			dps.add(dp);
		} // i->

		if (mands.size()>0){
			
			DeliveryPackage dp = new DeliveryPackage( dpItemIn ) ;
			
			dp.priority = dp.priority -1 ;
			dp.setGuid( GUID.randomvalue() ) ;
			
			dp.getQueryTerms().addAll( mands );
			dps.add(dp);
		}

		
		return dps;
	}
	
	

}
